<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 8/9/17
 * Time: 10:43 AM
 */

namespace common\components\tools;


use yii\httpclient\Client;
use yii\web\Response;

class Api
{
    /**
     * @param $uri
     * @param $data
     * @param string $method
     * @return Response
     */
    public static function request($uri, $data, $method = 'post', $url = null)
    {
        $url = $url?$url:\Yii::$app->params['api_domain'];
        $client = new Client([
            'baseUrl' => $url.$uri,
        ]);
        $token = \Yii::$app->params['admin_token'];
        $timestamp = time();
        $nonce = $timestamp.rand(1000,9999);
        $signature = md5($token.$timestamp.$nonce.\Yii::$app->params['admin_sign_key']);
        $response = $client->$method('', $data, [
            'token' => $token,
            'timestamp' => $timestamp,
            'nonce' => $nonce,
            'signature' => $signature,
        ])->send();
        return $response;
    }
}